package com.kotlin.hxh.ui.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.kotlin.base.utils.RouteTools
import com.kotlin.hxh.R
import com.kotlin.hxh.base.BaseMainFragment
import com.kotlin.hxh.event.TabSelectedEvent
import com.kotlin.hxh.ui.fragment.first.ZhihuFirstFragment
import com.kotlin.hxh.ui.fragment.first.child.FirstHomeFragment
import com.kotlin.hxh.ui.fragment.fourth.ZhihuFourthFragment
import com.kotlin.hxh.ui.fragment.fourth.child.MeFragment
import com.kotlin.hxh.ui.fragment.second.ZhihuSecondFragment
import com.kotlin.hxh.ui.fragment.second.child.ViewPagerFragment
import com.kotlin.hxh.ui.fragment.third.ZhihuThirdFragment
import com.kotlin.hxh.ui.fragment.third.child.ShopFragment
import com.kotlin.hxh.ui.view.BottomBar
import com.kotlin.hxh.ui.view.BottomBarTab
import kotlinx.android.synthetic.main.activity_hxh.*

import org.greenrobot.eventbus.EventBus

@Route(path = RouteTools.route_hxh)
class HxhActivity : AppCompatActivity(), BaseMainFragment.OnBackToFirstListener {

    companion object {
        @JvmField
        val FIRST = 0
        @JvmField
        val SECOND = 1
        @JvmField
        val THIRD = 2
        @JvmField
        val FOURTH = 3
    }


    private val mFragments = arrayOfNulls<Fragment>(4)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hxh)
        val firstFragment = findFragment(ZhihuFirstFragment::class.java)
        if (firstFragment == null) {
            mFragments[FIRST] = ZhihuFirstFragment.newInstance()
            mFragments[SECOND] = ZhihuSecondFragment.newInstance()
            mFragments[THIRD] = ZhihuThirdFragment.newInstance()
            mFragments[FOURTH] = ZhihuFourthFragment.newInstance()
            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FOURTH])
        } else {
            mFragments[FIRST] = firstFragment
            mFragments[SECOND] = findFragment(ZhihuSecondFragment::class.java)
            mFragments[THIRD] = findFragment(ZhihuThirdFragment::class.java)
            mFragments[FOURTH] = findFragment(ZhihuFourthFragment::class.java)
        }

        initView()

    }

    private fun initView() {
        bottomBar.addItem(BottomBarTab(this, R.drawable.ic_home_white_24dp))
                .addItem(BottomBarTab(this, R.drawable.ic_discover_white_24dp))
                .addItem(BottomBarTab(this, R.drawable.ic_message_white_24dp))
                .addItem(BottomBarTab(this, R.drawable.ic_account_circle_white_24dp))
        bottomBar.setOnTabSelectedListener(object : BottomBar.OnTabSelectedListener {
            override fun onTabSelected(position: Int, prePosition: Int) {
                showHideFragment(mFragments[position])
            }

            override fun onTabReselected(position: Int) {
                val currentFragment = mFragments[position]
                val count = currentFragment!!.childFragmentManager.backStackEntryCount
                // 如果不在该类别Fragment的主页,则回到主页;
                if (count > 1) {
                    if (currentFragment is ZhihuFirstFragment) {
                        currentFragment.popToChild(FirstHomeFragment::class.java, false)
                    } else if (currentFragment is ZhihuSecondFragment) {
                        currentFragment.popToChild(ViewPagerFragment::class.java, false)
                    } else if (currentFragment is ZhihuThirdFragment) {
                        currentFragment.popToChild(ShopFragment::class.java, false)
                    } else if (currentFragment is ZhihuFourthFragment) {
                        currentFragment.popToChild(MeFragment::class.java, false)
                    }
                    return
                }

                // 这里推荐使用EventBus来实现 -> 解耦
                if (count == 1) {
                    // 在FirstPagerFragment中接收, 因为是嵌套的孙子Fragment 所以用EventBus比较方便
                    // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
                    EventBus.getDefault().post(TabSelectedEvent(position))
                }
            }

            override fun onTabUnselected(position: Int) {
            }
        })
    }

    override fun onBackPressedSupport() {
        if (supportFragmentManager.backStackEntryCount > 1) {
            supportFragmentManager.popBackStack()
        } else {
            ActivityCompat.finishAfterTransition(this@HxhActivity)
        }
    }

    override fun onBackToFirstFragment() {
        bottomBar.setCurrentItem(0)
    }


}
